//
//  PlayHistoryViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-11.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "PlayHistoryViewController.h"
#import "HistoryCell.h"
#import "PlayerViewController.h"

@interface PlayHistoryViewController ()

@end

@implementation PlayHistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.historyToDeleteArray = [NSMutableArray arrayWithArray:0];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    nothingView.hidden = YES;
    [self updateDisplay];
}

- (void)updateDisplay
{
    self.historyArray = [[PlayerHistoryManager defaultManager] history];
    [historyTableView reloadData];
    if (_historyArray.count > 0) {
        nothingView.hidden = YES;
    }
    else {
        nothingView.hidden = NO;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.historyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryCell *cell = [historyTableView dequeueReusableCellWithIdentifier:@"HistoryCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:self options:nil] objectAtIndex:0];
        cell.historyToDeleteArray = _historyToDeleteArray;
    }
    cell.index = indexPath.row;
    cell.historyInfo = [self.historyArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (historyTableView.editing) {
        [historyTableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    else {
        selectedIndex = indexPath.row;
        [self playTheList];
        //跳转到播放界面
        PlayerViewController *playerViewController = [[[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil] autorelease];
        UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
        [navController pushViewController:playerViewController animated:YES];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)playTheList
{
    NSArray *playlist = [[_historyArray objectAtIndex:selectedIndex] valueForKey:@"playlist"];
    int index = [[[_historyArray objectAtIndex:selectedIndex] valueForKey:@"index"] intValue];
    int time = [[[_historyArray objectAtIndex:selectedIndex] valueForKey:@"time"] intValue];
    [[AudioPlayer defaultPlayer] setHistoryFowardTime:time];
    [[AudioPlayer defaultPlayer] playlist:playlist fromIndex:index];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:historyTableView.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (NSArray *)historyItemIds
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in _historyArray) {
        [array addObject:[dic objectForKey:@"item_id"]];
    }
    return array;
}

- (void)beginEdit
{
    [historyTableView setEditing:YES animated:YES];
}

- (void)completeEdit
{
    [historyTableView setEditing:NO animated:YES];
    [_historyToDeleteArray removeAllObjects];
}

- (void)selectAll
{
    if (_historyToDeleteArray.count == _historyArray.count) {
        [_historyToDeleteArray removeAllObjects];
    }
    else {
        [_historyToDeleteArray removeAllObjects];
        [_historyToDeleteArray addObjectsFromArray:[self historyItemIds]];
    }
    [historyTableView reloadData];
}

- (void)deleteSelected
{
    if (_historyToDeleteArray.count == [[[PlayerHistoryManager defaultManager] history] count]) {
        [[PlayerHistoryManager defaultManager] removeAllPlaylist];
    }
    else {
        for (NSString *itemid in _historyToDeleteArray) {
            [[PlayerHistoryManager defaultManager] removePlaylist:itemid];
        }
        [[PlayerHistoryManager defaultManager] save];
    }
    [historyTableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [historyTableView release];
    [_historyArray release];
    [nothingView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [historyTableView release];
    historyTableView = nil;
    [nothingView release];
    nothingView = nil;
    [super viewDidUnload];
}
@end
